package com.xblog.chatbox.vlmp.web;

import com.xblog.chatbox.vlmp.conversation.Conversation;
import com.xblog.chatbox.vlmp.conversation.ConversationPayload;
import com.xblog.chatbox.vlmp.conversation.ConversationPayloadFeedbacks;
import com.xblog.chatbox.vlmp.conversation.ConversationRequest;
import com.xblog.chatbox.vlmp.service.VlmpAgentService;
import com.xiaowu.xblog.constant.AuthorizationContext;
import com.xiaowu.xblog.model.PagerResult;
import com.xiaowu.xblog.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/8/6 0:02
 */
@RestController
@Tag(name = "VlmpAgent相关接口")
@RequestMapping("/vlmp/agent")
@RequiredArgsConstructor
public class VlmpAgentController  {

    private final VlmpAgentService vlmpAgentService;

    @Operation(summary = "智能体对话")
    @PostMapping(value = "/v1/chat/completions", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Result<Flux<String>> completion(@RequestBody ConversationRequest conversationRequest){
        return vlmpAgentService.completions(AuthorizationContext.getContext(),conversationRequest);
    }

    /**
     * 获取会话信息
     */
    @GetMapping("/conversation/info")
    @Operation(summary = "获取会话信息")
    public Result<Conversation> getConversation(@Parameter(description = "会话Id") @RequestParam String conversationId) {
        Conversation conversation = vlmpAgentService.getConversation(conversationId);
        return Result.success(conversation);
    }

    /**
     * 获取当前登录用户会话列表
     */
    @GetMapping("/conversation/getList")
    @Operation(summary = "获取当前登录用户会话列表")
    public Result<List<Conversation>> getConversationList(@Parameter(description = "会话主题") @RequestParam String topic) {
        List<Conversation> conversationList = vlmpAgentService.getConversationList(AuthorizationContext.getContext(), topic);
        return Result.success(conversationList);
    }

    /**
     * 删除会话信息
     */
    @DeleteMapping("/conversation/delete")
    @Operation(summary = "删除会话信息")
    public void delete(@Parameter(description = "会话ID") @RequestParam String guid) {
        vlmpAgentService.delete(AuthorizationContext.getContext(), guid);
    }

    /**
     * 重命会话信息
     */
    @PostMapping("/conversation/rename")
    @Operation(summary = "重命名会话信息")
    public void rename(@RequestBody Conversation conversation) {
        vlmpAgentService.rename(AuthorizationContext.getContext(), conversation);
    }

    /**
     * 获取当前登录用户获取最新会话列表
     */
    @GetMapping("/conversation/getLatestList")
    @Operation(summary = "获取当前登录用户获取最新会话列表")
    public Result<List<Conversation>> getLatestConversationList(@Parameter(description = "会话主题") @RequestParam String topic,
                                                        @Parameter(description = "获取最近多少条数据") @RequestParam int size) {
        List<Conversation> latestConversationList = vlmpAgentService.getLatestConversationList(AuthorizationContext.getContext(), topic, size);
        return Result.success(latestConversationList);
    }

    /**
     * 根据会话ID获取聊天内容
     */
    @GetMapping("/conversation/payload/getList")
    @Operation(summary = "根据会话ID获取聊天内容")
    public Result<PagerResult<ConversationPayload>> getConversationPayloadList(@Parameter(description = "会话ID") @RequestParam String conversationId,
                                                                       @Parameter(description = "会话时间") @RequestParam(required = false) String oldestPayloadId
    ) {
        PagerResult<ConversationPayload> conversationPayloadPagerResult= vlmpAgentService.getConversationPayloadList(AuthorizationContext.getContext(), conversationId, oldestPayloadId);
        return Result.success(conversationPayloadPagerResult);
    }


    /**
     * 根据会话ID获取最近的聊天内容
     */
    @GetMapping("/conversation/payload/getLatestList")
    @Operation(summary = "根据会话ID获取最近的聊天内容")
    public Result<List<ConversationPayload>> getLatestConversationPayloadList(@Parameter(description = "会话ID") @RequestParam String conversationId,
                                                                      @Parameter(description = "获取最近多少条数据") @RequestParam int size
    ) {
        List<ConversationPayload> latestConversationPayloadList = vlmpAgentService.getLatestConversationPayloadList(AuthorizationContext.getContext(), conversationId, size);
        return Result.success(latestConversationPayloadList);
    }

    /**
     * 反馈当前对话内容
     */
    @PostMapping("/conversation/payload/feedbacks")
    @Operation(summary = "反馈当前对话内容")
    public void feedbacks(@RequestBody ConversationPayloadFeedbacks feedbacks) {
        vlmpAgentService.feedbacks(AuthorizationContext.getContext(), feedbacks);
    }
}
